
var Wizard = Class.extend({
	index : 0 ,
	steps : [] ,
	call : null ,
	init : function(options , call) {
		this.steps = options;
		this.call = call;
	} ,
	active : function(name){
		return this.steps[this.index].name == name;
	} ,
	validated : function(name){
		for( var i = 0 ; i < this.steps.length ; i++) {
			var step = this.steps[i];
			if(step.name == name) {
				return step.validated;
			}
		}
		return false;
	} ,
	click : function(i){
		this.index = i;
	} ,
	prev : function(){
		if( this.index == 0) {
			return;
		}
		this.index--;
	} ,
	next : function(){
		if( this.steps.length -1 == this.index) {
			return;
		}
		if( this.call ) {
			var validated = this.call(this.steps[this.index]);
			if( validated ) {
				this.steps[this.index].validated = true;
				this.index++;
			}
			
		}  else {
			this.index++;
		}
	} ,
	current : function(){
		return this.steps[this.index];
	}
});

var DynamicTable = Class.extend({

	init : function(settings , data){
		this.settings = settings || {};
		this.data = data || [] ;
		this.object = null;
		this._initData();	
	} , 
	config : function( settings ){
		this.settings = settings || {};
		this.object = null;
	} ,
	refresh : function(data) {
		this.data = data || [];
		this.object = null;
		this._initData();
	} ,
	_initData : function(){
		var tempData = this.data;
		this.data = [];
		for (var i = 0 ; i < tempData.length; i++) {
			var d = tempData[i];
			d['__id__'] = i;
			this.data.push(d);
		};
	} ,
	save : function(){
		var object = this.object;
		if( !object) {
			return ;
		}
		if( this.settings['validate']  ) {
			if( !this.settings['validate'](object)) {
				return;
			}
		}
		var id = object['__id__'];
		if( this.data[id]) {
			this.data[id] = object ;
		} else {
			object['__id__'] = this.data.length;
			this.data.push(object);
		}
		this.object = null;
	} ,
	add : function(callback ) {
		var object = {};
		if( callback ) {
			callback(object);
		}
		if( this.settings['add']) {
			this.settings['add'](object);
		}
		if( !object ) {
			return ;
		}
		this.object = object;
	} , 
	edit : function(object , callback){
		if( !object ) {
			return ;
		}
		if( callback ) {
			callback(object);
		}
		if( this.settings['edit']) {
			this.settings['edit'](object);
		}
		this.object = object;
	} , 
	remove : function(object , callback) { 
		if( !object ) {
			return ;
		}
		if( callback ) {
			callback(object);
		}
		if( this.settings['remove']) {
			this.settings['remove'](object);
		}
		var id = object['__id__'];
		if( this.data[id]) {
			this.data.splice( id , 1);
		}
		this.object = null;
	}
});

var BindEvents = Class.extend({
	init : function(controller , events){
		for( var i = 0 ; i < events.length ; i++) {
			var event = events[i];
			controller.$scope[event] = controller[event].bind(controller);	
		}
	} ,
});

var CopyJSON  = Class.extend({
	init : function(props){
		this.props = props ||  [];
	} ,
	copy : function(data , props){
		var properties = props || this.props;
		var object = {} ;
		for( var i = 0 , length = properties.length ; i < length ; i++) {
			var prop = properties[i];
			object[prop] = data[prop] || "";
		}
		return object;
	}
});

var AlumniController = FormController.extend({
	model   : null ,
	params  : null ,
	alumnus : null , 
	data : null ,
	init: function($scope , $routeParams , alumniCommandModel){
		this.params = $routeParams;
		this.model = alumniCommandModel;
		this._super($scope);
	},
	defineScope : function(){
		var stepSettings = [ 
			{ name : "1" , title : "基本信息"}  ,
			{ name : "2" , title : "学籍信息" , save : 'saveEducation'  }  ,
			{ name : "3" , title : "单位信息" , save : 'saveCompany' }  ,
			{ name : "4" , title : "校友状态" , save : 'saveAssociation' }  ,
			{ name : "5" , title : "捐赠项" , save : 'saveDonate' } ,
			{ name : "6" , title : "活动" , save : 'saveActivity'  }  ,
			{ name : "7" , title : "头像" , save : 'savePicture' } 
		];

		this.$scope.wizard = new Wizard(stepSettings);
		this.$scope.educationTable = new DynamicTable();
		this.$scope.companyTable = new DynamicTable();
		this.$scope.donateTable = new DynamicTable(); 
		this.$scope.activityTable = new DynamicTable(); 
		this.$scope.basicInfo = {
			gendar : 'MALE' ,
			beliving : true 
		} ;
		new BindEvents( this , [
			'saveBasicInfo' , 'save' , 'prev' , 'saveAndNext'
		]);
	}  ,
	saveBasicInfo : function(){
		var props = [
			'name' ,'usedName','identificationType','identificationNumber','countryOfLocation',
			'stateOfLocation' , 'cityOfLocation' ,'cityOfPostalAddress','cityOfPostalAddress',
			'stateOfPostalAddress','postalcodeOfPostalAddress', 'countyOfPostalAddress' ,
			'streetOfPostalAddress','homeTelephone','officeTelephone','cellphone','email' ,
			'qq' ,'weibo','wechat','alive','gendar','politicalStatus' ,
			'birthday' ,'origin','nation'
		];
		var copy = new CopyJSON(props);
		var info  = copy.copy(this.$scope.basicInfo);
		this.model.createPeronsalInformation(info, function(data){
			this.$scope.alumusId = data;
			// console.log(data);
			this.$scope.wizard.next();
		}.bind(this));
	} ,
	saveEducation : function(){
		var props = [
			'studentId' ,'dateAttendedStarts','dateAttendedEnds','department','major',
			'className' , 'educationQuality' ,'degree','schoolingSpan',
			'enrollType','tuitor'
		];
		var data = this.$scope.educationTable.data;
		this._copyAndSaveToServer('addEducations' , props , data , function(){
			// this.$scope.wizard.next();
		}.bind(this) );
	} ,
	saveCompany : function(){
		var props = [
			'companyName' ,'dateAttendedBegins','dateAttendedEnds',
			'companyType','industry','position' , 'location'
		];
		var data = this.$scope.companyTable.data;
		this._copyAndSaveToServer('addWorks' , props , data , function(){
			// this.$scope.wizard.next();
		}.bind(this) );
	} ,
	saveAssociation : function(){
		var props = [
			'association' ,'position','remark'
		];

		var copy = new CopyJSON(props);
		var info  = copy.copy(this.$scope.association);
		this.model.assignAssociation(this.$scope.alumusId , info, function(data){
		}.bind(this));
	} ,
	saveDonate : function(){
		var props = [
			'project' ,'amount','date','remark'
		];
		var data = this.$scope.donateTable.data;
		this._copyAndSaveToServer('addDonations' , props , data , function(){
			// this.$scope.wizard.next();
		}.bind(this) );
	} ,
	saveActivity : function(){
		var props = [
			'name' ,'attendedDate'
		];
		var data = this.$scope.activityTable.data;
		this._copyAndSaveToServer('addActivities' , props , data , function(){
		}.bind(this) );
	} ,
	savePicture : function(){
		this.model.addAvatar(this.$scope.alumusId  , file.files[0] , function(){
//			console.log('aaa');
		}.bind(this));
	} ,
	save : function() {
		var step = this.$scope.wizard.current();
		if(step.save && this[step.save] ) {
			this[step.save]();
		}
	} ,
	saveAndNext : function(){
		this.save();
		this.$scope.wizard.next();
	} ,
	prev : function(){
		this.$scope.wizard.prev();
	} , 
	_copyAndSaveToServer : function(name , props , arr , successcb, errorcb){
		var copy = new CopyJSON(props);
		var objects = [];
		for (var i = 0; i < arr.length ; i++) {
			var object = copy.copy(arr[i]);
			objects.push( object);
		};

		if( !this.$scope.alumusId ) {
			if( errorcb) {
				errorcb();
			}
		}

		if( this.model[name]) {
			this.model[name](this.$scope.alumusId  , objects , successcb , errorcb);
		} else {
			if( errorcb) {
				errorcb();
			}
		}		
	} ,
});

AlumniController.$inject = [ '$scope', '$routeParams', 'AlumniCommandModel' ];